package com.scada.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.MemberFamilyVisitorClient;
import com.scada.model.bean.MemberFamily;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.MemberFamilyVO;
import com.scada.model.dataTable.MemberFamilyList;
import com.scada.model.result.ResponseBean;

@Controller
@RequestMapping("memberFamily")
public class MemberFamilyController extends BaseController{

	@Autowired
	private MemberFamilyVisitorClient memberFamilyVisitorClient;
	
	@RequestMapping(value = "toMemberFamilyPageData")
	public String toMemberFamilyPageData(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return "member/family/list";
	}
	
	@RequestMapping(value = "getMemberFamilyPageData")
	@ResponseBody
	public String getMemberFamilyPageData(Long memberId){
		
		List<MemberFamilyList> list =null;
        ResponseBean rb = memberFamilyVisitorClient.getMemberFamilyByMemberId(MemberFamily.IsShowCode.YES.getKey(), memberId);
        if(StringUtils.equals("2", rb.getType())){
        	list = (List<MemberFamilyList>)rb.getObject("result");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("dataList", list);
		return toJson(map);
	}
	
	@RequestMapping(value = "toSaveMemberFamilyForm")
	public String toSaveMemberFamilyForm(Long memberId, ModelMap model){
		
		model.put("memberId", memberId);
		return "member/family/add";
	}
	
	@RequestMapping(value = "insertMemberFamily")
	@ResponseBody
	public String insertMemberFamily(@ModelAttribute MemberFamily memberFamily) {
		
		ResponseBean rb = new ResponseBean();
		User user = getCurrentUser();
		memberFamily.setCreateId(user.getId());
		memberFamily.setLastUpdateId(user.getId());
		rb = memberFamilyVisitorClient.insertMemberFamily(memberFamily);
		return toJson(rb);
	}
	
	@RequestMapping(value = "toEditMemberFamilyForm")
	public ModelAndView toEditMemberFamilyForm(Long id, ModelMap model){
		
		MemberFamilyVO mf = null;
		try {
			ResponseBean rb = memberFamilyVisitorClient.getMemberFamilyById(id);
			if(StringUtils.equals("2", rb.getType())){
				mf = (MemberFamilyVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mf", mf);
		return new ModelAndView("member/family/edit");
	}
	
	@RequestMapping(value = "updateMemberFamily")
	@ResponseBody
	public String updateMemberFamily(@ModelAttribute MemberFamily memberFamily) {
		
		ResponseBean rb = new ResponseBean();
		User user = getCurrentUser();
		memberFamily.setLastUpdateId(user.getId());
		rb = memberFamilyVisitorClient.updateMemberFamily(memberFamily);
		return toJson(rb);
	}
	
	@RequestMapping(value = "deleteMemberFamilyById")
	@ResponseBody
	public String deleteMemberFamilyById(Long id){
		
		return toJson(memberFamilyVisitorClient.deleteMemberFamilyById(MemberFamily.IsShowCode.NO.getKey(), id));
	}
}
